﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Collections;
using System.Xml.Linq;

namespace DeThiTracNghiem
{
    public partial class FormSoanCauHoi : Form
    {
        public FormSoanCauHoi()
        {
            InitializeComponent();
        }
        
        private CauHoiTracNghiem_LinQDataContext data = new CauHoiTracNghiem_LinQDataContext();
        private ArrayList arrRbt = new ArrayList();
        private ArrayList arrTextbox = new ArrayList();

        private void Form1_Load(object sender, EventArgs e)
        {
            arrRbt.Add(RbtnA);
            arrRbt.Add(RbtnB);
            arrRbt.Add(RbtnC);
            arrRbt.Add(RbtnD);

            arrTextbox.Add(textBoxA);
            arrTextbox.Add(textBoxB);
            arrTextbox.Add(textBoxC);
            arrTextbox.Add(textBoxD);

            LoadGridView();
            LoadComboBoxChuDe();
            Clear();
        }

        private void LoadGridView()
        {
            var dscauhoi = data.CauHois.Select(c => new { c.MaCauHoi, c.NoiDung });
            dgvCauHoi.DataSource = dscauhoi;
            dgvCauHoi.Columns[0].Visible = false;
            dgvCauHoi.Columns[1].Width = 500;
            dgvCauHoi.ClearSelection();
        }

        private void LoadComboBoxChuDe()
        {
            var dschude = data.ChuDes.Select(cd => cd);
            comboBoxChuDe.DataSource = dschude;
            comboBoxChuDe.DisplayMember = "TenChuDe";
            comboBoxChuDe.ValueMember = "MaChuDe";
        }

        private void Clear()
        {
            comboBoxChuDe.SelectedIndex = -1;
            textBoxNoiDung.Clear();
            foreach (TextBox txt in arrTextbox)
                txt.Clear();
            foreach (RadioButton rbt in arrRbt)
                rbt.Checked = false;
        }

        private void GanCauHoi(CauHoi ch)
        {
            ch.MaChuDe = int.Parse(comboBoxChuDe.SelectedValue.ToString());
            ch.NoiDung = textBoxNoiDung.Text;
            ch.DapAnA = textBoxA.Text;
            ch.DapAnB = textBoxB.Text;
            ch.DapAnC = textBoxC.Text;
            ch.DapAnD = textBoxD.Text;
            ch.DapAnDung = DapAnDung();
        }

        private void buttonThem_Click(object sender, EventArgs e)
        {
            if (KiemTraDuLieu() == 1)
            {
                CauHoi ch = new CauHoi();
                GanCauHoi(ch);
                data.CauHois.InsertOnSubmit(ch);
                data.SubmitChanges();
                Form1_Load(sender, e);
            }
        }

        private int KiemTraDuLieu()
        {
            if (comboBoxChuDe.SelectedIndex == -1)
            {
                MessageBox.Show("Chưa chọn Chủ đề", "Thông báo");
                return 0;
            } 

            if (textBoxNoiDung.Text == "")
            {
                MessageBox.Show("Chưa nhập nội dung", "Thông báo");
                textBoxNoiDung.Focus();
                return 0;
            }
            
            foreach (TextBox txt in arrTextbox)
                if (txt.Text == "")
                {
                    MessageBox.Show("Chưa nhập đủ đáp án");
                    txt.Focus();
                    return 0;
                }

            if (DapAnDung() == 0)
            {
                MessageBox.Show("Chưa chọn Đáp án Đúng");
                return 0;
            }

            return 1;
        }

        private int DapAnDung()
        {
            foreach (RadioButton rbt in arrRbt)
                if (rbt.Checked == true)
                    return int.Parse(rbt.Tag.ToString());
            return 0;
        }

        private int macauhoi;

        private void dgvCauHoi_SelectionChanged(object sender, EventArgs e)
        {
            if (dgvCauHoi.Rows.Count>0)
            {
                try
                { 
                    macauhoi = int.Parse(dgvCauHoi.CurrentRow.Cells[0].Value.ToString());
                    var ch = data.CauHois.Single(c => c.MaCauHoi == macauhoi);
                    comboBoxChuDe.SelectedValue = int.Parse(ch.MaChuDe.ToString());
                    textBoxNoiDung.Text = ch.NoiDung.ToString();
                    textBoxA.Text = ch.DapAnA.ToString();
                    textBoxB.Text = ch.DapAnB.ToString();
                    textBoxC.Text = ch.DapAnC.ToString();
                    textBoxD.Text = ch.DapAnD.ToString();
                    foreach (RadioButton rbt in arrRbt)
                        if (int.Parse(rbt.Tag.ToString()) == ch.DapAnDung)
                            rbt.Checked = true;
                }
                catch (Exception)
                {

                }
            }
        }

        private void buttonXoa_Click(object sender, EventArgs e)
        {
            var ch = data.CauHois.Single(c => c.MaCauHoi == macauhoi);
            data.CauHois.DeleteOnSubmit(ch);
            data.SubmitChanges();
            Form1_Load(sender, e);
        }

        private void buttonCapNhat_Click(object sender, EventArgs e)
        {
            if (KiemTraDuLieu() == 1)
            {
                var ch= data.CauHois.Single(c => c.MaCauHoi == macauhoi);
                GanCauHoi(ch);
                data.SubmitChanges();
                Form1_Load(sender, e);
            }
        }

        private void buttonThoat_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void buttonGhi_Click(object sender, EventArgs e)
        {
            string src;
            SaveFileDialog save = new SaveFileDialog();
            save.Filter = ".xml|*.xml";
            
            if (save.ShowDialog() == DialogResult.OK)
            {
                src = save.FileName;
                XDocument doc = new XDocument();
                XComment comment = new XComment("Danh Sach Cau Hoi");
                doc.Add(comment);

                XElement danhsach = new XElement("DanhSach");
                doc.Add(danhsach);

                var ds = data.CauHois.Select(c => c);
                foreach (CauHoi ch in ds)
                {
                    XElement cauhoi = new XElement("CauHoi");
                    XAttribute chude = new XAttribute("ChuDe", ch.MaChuDe);
                    cauhoi.Add(chude);

                    XElement noidung = new XElement("NoiDung", ch.NoiDung);
                    XElement dapana = new XElement("DapAnA", ch.DapAnA);
                    XElement dapanb = new XElement("DapAnB", ch.DapAnB);
                    XElement dapanc = new XElement("DapAnC", ch.DapAnC);
                    XElement dapand = new XElement("DapAnD", ch.DapAnD);
                    XElement dapandung = new XElement("DapAnDung", ch.DapAnDung);

                    cauhoi.Add(noidung);
                    cauhoi.Add(dapana);
                    cauhoi.Add(dapanb);
                    cauhoi.Add(dapanc);
                    cauhoi.Add(dapand);
                    cauhoi.Add(dapandung);

                    danhsach.Add(cauhoi);
                }
                doc.Save(src);
            }
        }

        private void buttonDoc_Click(object sender, EventArgs e)
        {
            string src = "";
            OpenFileDialog open = new OpenFileDialog();        
            if (open.ShowDialog() == DialogResult.OK)
            {
                src = open.FileName;
                XDocument doc = new XDocument();
                doc = XDocument.Load(src);
                var dscauhoi = from ch in doc.Element("DanhSach").Elements("CauHoi")
                                select ch;
               
                foreach (var ch in dscauhoi)
                {
                    CauHoi cauhoi = new CauHoi();
                    cauhoi.MaChuDe = int.Parse(ch.Attribute("ChuDe").Value.ToString());
                    cauhoi.NoiDung = ch.Element("NoiDung").Value;
                    cauhoi.DapAnA = ch.Element("DapAnA").Value;
                    cauhoi.DapAnB = ch.Element("DapAnB").Value;
                    cauhoi.DapAnC = ch.Element("DapAnC").Value;
                    cauhoi.DapAnD = ch.Element("DapAnD").Value;
                    cauhoi.DapAnDung = int.Parse(ch.Element("DapAnDung").Value);
                    data.CauHois.InsertOnSubmit(cauhoi);
                    data.SubmitChanges();
                }
                
                Form1_Load(sender, e);
            }  
        }
    }
}
